package com.xielx.ris.statistic.excel;

import com.xielx.ris.statistic.bean.ReportRow;
import com.xielx.ris.statistic.enums.Platform;
import com.xielx.ris.statistic.enums.SheetColumnConfig;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelHandler {
        private HSSFWorkbook workbook;

        public ExcelHandler(HSSFWorkbook workbook) {
                this.workbook = workbook;
        }

        public static ExcelHandler getInstance(File path) throws IOException {
                try (FileInputStream fis = new FileInputStream(path)) {
                        return new ExcelHandler(new HSSFWorkbook(fis));
                }
        }

        public static ExcelHandler getInstance(String path) throws IOException {
                try (FileInputStream fis = new FileInputStream(path)) {
                        return new ExcelHandler(new HSSFWorkbook(fis));
                }
        }

        public List<ReportRow> readExcel() {
                List<ReportRow> list = new ArrayList<>();
                for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
                        HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
                        Map<Integer, SheetColumnConfig> configMap = SheetColumnConfig.createReportRow(sheetIndex);
                        System.out.println("==========================开始解析【" + sheet.getSheetName() + "】==========================================");
                        for (int rowNum = 1; rowNum < 32; rowNum++) {
                                HSSFRow row = sheet.getRow(rowNum);
                                String sfmc = row.getCell(1).toString();
                                String sfdm = Platform.findZh_sf(sfmc);
                                System.out.print(sfmc + "\t\t\t");
                                for (int columnNum = 2; columnNum < row.getLastCellNum(); columnNum++) {
                                        try {
                                                double value = row.getCell(columnNum).getNumericCellValue();
                                                ReportRow reportRow = new ReportRow(sfdm);
                                                SheetColumnConfig config = configMap.get(columnNum);
                                                if (null == config) {
                                                        continue;
                                                }
                                                reportRow.setSjlx(config.getCode());
                                                reportRow.setTjsl((int) value);
                                                list.add(reportRow);
                                                System.out.print(value + "\t\t\t");
                                        } catch (RuntimeException e) {
                                                System.out.println(String.format("出错在sheet[%s]：%s行，%s列", sheet.getSheetName(),rowNum, columnNum));
                                                throw e;
                                        }
                                }
                                System.out.println();
                        }
                        System.out.println();
                }
                return list;
        }
}
